﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System.Text.RegularExpressions;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class ModulePage : WatiNBase
    {
        #region Constructors

        public ModulePage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        public ModulePage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region TextFields
        public TextField ModuleTextBox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(new Regex(".*_EditHTML_txtContent_TxtDesktopHTML", RegexOptions.IgnoreCase)));
                }
                return IEInstance.TextField(Find.ById(new Regex(".*_EditHTML_txtContent_TxtDesktopHTML", RegexOptions.IgnoreCase)));
            }
        }
        public TextField ModuleTitleTextField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ModuleSettings_txtTitle")));
                }
                return ContentDiv.TextField(Find.ById(s => s.EndsWith("ModuleSettings_txtTitle")));
            }
        }
        public TextField AddUserPermissionField
        {
            get { return UserPermissionsDiv.TextField(Find.ByName(s => s.Contains("ModuleSettings$dgPermissions$txtUser"))); }
        }
        public TextField StartDateField
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ModuleSettings_startDatePicker_dateInput_ClientState")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("ModuleSettings_startDatePicker_dateInput_ClientState")));
            }
        }
        public TextField EndDateField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("ModuleSettings_endDatePicker_dateInput_text")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("ModuleSettings_endDatePicker_dateInput_text")));
            }
        }
        #endregion

        #region Spans
       
        #endregion

        #region Links
        public Link SaveContentLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Save"));
                }
                return IEInstance.Link(Find.ByTitle("Save"));
            }
        }
        public Link AddUserPermissionButton
        {
            get
            {
                return UserPermissionsDiv.Link(Find.ByText(s => s.Contains("Add")));
            }
        }
        public Link UpdateLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Update"));
                }
                return IEInstance.Link(Find.ByTitle("Update"));
            }
        }
        public Link DeleteLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Delete"));
                }
                return ContentDiv.Link(Find.ByTitle("Delete"));
            }
        }
        public Link EditModuleContentLink
        {
            get { return ActionMenuBody.Link(Find.ByText(s => s.Contains("Edit Content"))); }
        }
        public Link CurrentContentLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText(s => s.Contains("Current Content")));
                }
                return IEInstance.Link(Find.ByText(s => s.Contains("Current Content")));
            }
        }

        public Link PageSettingsTabLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText(s => s.Contains("Page Settings")));
                }
                return IEInstance.Link(Find.ByText(s => s.Contains("Page Settings")));
            }
        }
        public Link PrintIcon
        {
            get { return ActionMenuBody.Link(Find.ByText(s => s.Contains("Print"))); }
        }
        public Link SyndicateIcon
        {
            get { return ActionMenuBody.Link(Find.ByText(s => s.Contains("Syndicate"))); }
        }
        public Link ModuleOnlineHelpLink
        {
            get { return ActionMenuBody.Link(Find.ByText(s => s.Contains("Online Help"))); }
        }
        #endregion

        #region Elements
        public Element PageContent
        {
            get { return IEInstance.Element(Find.ByClass("content_pad")); }
        }
        #endregion

        #region Images
        public Image ModuleActionMenuImage
        {
            get { return IEInstance.Image(Find.ById(s => s.Contains("dnnACTIONS_ctldnnACTIONSicn"))); }
        }
        public Image DeleteModuleImage
        {
            get { return IEInstance.Image(Find.BySrc(s => s.EndsWith("action_delete.gif"))); }
        }
        public Image AdvancedSettingsExpandButton
        {
            get { return IEInstance.Image(Find.ById(s => s.EndsWith("ModuleSettings_dshSecurity_imgIcon"))); }
        }
        public Image PageSettingsExpandIcon
        {
            get { return IEInstance.Image(Find.ById(s => s.EndsWith("ModuleSettings_dshPage_imgIcon"))); }
        }
        
        public Image PageSettingsAdvancedSettingsExpandIcon
        {
            get { return IEInstance.Image(Find.ById(s => s.EndsWith("ModuleSettings_dshOther_imgIcon"))); }
        }
        #endregion

        #region Divs
        public Div UserPermissionsDiv
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Div(Find.ByClass("dnnGrid dnnPermissionsGrid"));
                }
                return IEInstance.Div(Find.ByClass("dnnGrid dnnPermissionsGrid"));
            }
        }
        public Div ModuleTitleDiv
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("dnnTITLE_lblTitle_tb"))); }
        }
        public Div InlineEditButton
        {
            get { return ModuleTitleDiv.Div(Find.ById(s => s.EndsWith("dnnTITLE_lblTitle_tbedit"))); }
        }
        public Div ContainerDiv
        {
            get { return IEInstance.Div(Find.ByClass(s => s.StartsWith("c_container "))); }
        }
        #endregion

        #region CheckBoxes
        
        public CheckBox InheritPagePermissionsCheckBox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkInheritPermissions")));
                }
                return IEInstance.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkInheritPermissions")));
            }
        }
        public CheckBox DisplayOnAllPagesCheckbox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkAllTabs")));
                }
                return IEInstance.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkAllTabs")));
            }
        }
        public CheckBox PrintCheckBox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkDisplayPrint")));
                }
                return IEInstance.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkDisplayPrint")));
            }
        }
        public CheckBox SyndicateCheckBox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkDisplaySyndicate")));
                }
                return IEInstance.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkDisplaySyndicate")));
            }
        }
        public CheckBox DisplayAdminBorderCheckbox
        {
            get { return ContentPane.CheckBox(Find.ById(s => s.EndsWith("ModuleSettings_chkAdminBorder"))); }
        }
        #endregion

        #region SelectLists
        public SelectList ModuleContainerSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ModuleSettings_moduleContainerCombo")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ModuleSettings_moduleContainerCombo")));
            }
        }
        public SelectList MoveToPageSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("ModuleSettings_cboTab")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("ModuleSettings_cboTab")));
            }
        }
        #endregion

        #region Tables
        public Table ModulePermissionTable
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Div(Find.ById(s => s.EndsWith("ModuleSettings_permissionsRow"))).Table(Find.Any);
                }
                return IEInstance.Div(Find.ById(s => s.EndsWith("ModuleSettings_permissionsRow"))).Table(Find.Any);
            }
        }

        #endregion

        #endregion

        #region Public Methods

        public Image GetActionMenuImageForModule(int moduleNum)
        {
            return IEInstance.Images.Filter(Find.ById(s => s.Contains("dnnACTIONS_ctldnnACTIONSicn")))[moduleNum];
        }

        public void SelectTextEditorStyleByName(IE ieInstance, string selectedValue)
        {
            RadioButtonCollection rbCollection;
            if(PopUpFrame!= null)
            {
                rbCollection = PopUpFrame.RadioButtons;
            }
            else
            {
                rbCollection = ieInstance.RadioButtons;
            }
            foreach (RadioButton rb in rbCollection)
            {
                if (selectedValue != rb.GetAttributeValue("value")) continue;
                //rb.Checked = true;
                rb.Click();
                break;
            }
        }

        public Link GetEditModuleContentLink(int moduleNum)
        {
            //Returns the edit module content link for the module specified by moduleNum
            //0 being the first module on the page
            DivCollection actionMenu = IEInstance.Divs.Filter(Find.ByClass("dnnActionMenu"));
            if(actionMenu.Count > moduleNum)
            {
                return actionMenu[moduleNum].Link(Find.ByText(s => s.Contains("Edit Content")));
            }
            return IEInstance.Link(Find.ByText(s => s.Contains("Edit Content")));
        }

        public void AddContentToModule(string contentString, int moduleNum)
        {

            GetEditModuleContentLink(moduleNum).ClickNoWait();
            System.Threading.Thread.Sleep(2500);
            CurrentContentLink.Click();
            SelectTextEditorStyleByName(IEInstance, "BASIC");
            System.Threading.Thread.Sleep(3500);
            ModuleTextBox.TypeText(contentString);
            SaveContentLink.ClickNoWait();
        }

        public Link GetModuleSettingsImage(int moduleNum)
        {
            //Returns the edit module content link for the module specified by moduleNum
            //0 being the first module on the page
            return IEInstance.Divs.Filter(Find.ByClass(s => s.Contains("dnnActionMenu")))[moduleNum].Link(Find.ByText("Settings"));
        }

        public void ClickAddedUserEditCheckbox(string edition)
        {
            Image editCheckbox;
            Table table;
            if(edition.Equals("Community"))
            {
                table = UserPermissionsDiv.Tables[1];
                editCheckbox = table.TableRows.Filter(Find.ByClass("dnnGridItem"))[0].TableCells[2].Image(Find.Any);
            }
            else
            {
                editCheckbox = UserPermissionsDiv.Tables[1].TableRows.Filter(Find.ByClass("dnnGridItem"))[0].TableCells[7].Image(Find.Any);
            }
            editCheckbox.ClickNoWait();
        }

        public void ClickEditPermissionForRole(string roleName)
        {
            CheckBox result = null;
            foreach (TableRow row in ModulePermissionTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(roleName))
                {
                    result = row.TableCells[2].CheckBox(Find.Any);
                    break;
                }
                continue;
            }
            result.Click();
        }

        #endregion
    }
}
